var ProdCate = function() {

	var left_margin_step = 15;
	
	$("#addProduct").live(
			'click',
			function() {
				var pk_org = $("#vendorOrg").val();
				var pk_group = $("#vendorGroup").val();
				if((pk_org==null||pk_org=="")){
					showCfmMsg(getLanMsgs("0ec22180-100163"), getLanMsgs("0ec22180-100165"));
					return;
				}
				
				var myts = new Date();
				var formStr = $('#prod_class_filter').serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/register/qryTopLevelMarBasCls?myts=" + myts +"&pk_org=" + pk_org + "&pk_group="+pk_group,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						if (result != null && result != "[]") {
							var arr = eval(result);
							$("#prodcate_tree_list").empty();
							for ( var loop = 0; loop < arr.length; loop++) {
								var obj= arr[loop];
								var a = $("<a></a>").attr(
														"href", "#").addClass(
														"prodcate_category_trigger")
														.attr("margin", 0);
								var pk = $("<input />").val(obj.pk_marbasclass).attr("type", "hidden").attr("name", "prodcate_intree");
								var inputObj = $("<input/>")
														.val(obj.pk_marbasclass)
														.attr("type", "checkbox").attr("name", "pkProdcateCheckbox");
								if(obj.isLeaf == "N"){
									inputObj.attr("disabled", "true");
								}
								var label = $("<label></label>")
														.text(obj.name).attr("title", "prodcatename").attr("qaDesc", obj.qaDescription);
								
								var div = $("<div></div>")
														.addClass("prodcate_tree_category")
														.append(a).append(pk).append(inputObj).append(label);
								var existProdCl = $("#complateProducts").find("[name='pkProdCl']");
								if(existProdCl != null && existProdCl.length !=0){
									for(var l=0; l<existProdCl.length; l++){
										if($(existProdCl[l]).val() == arr[loop].pk_marbasclass){
											break;
										}
									}
									if(l == existProdCl.length){
										$("#prodcate_tree_list").append(div);
									}
								}else{
									$("#prodcate_tree_list").append(div);
								}
							}
							$("#prodcatePicker").find("#prodPkChecked").find("input").remove();
						}else{
							$("#prodcate_tree_list").empty();
						}
					}
				});
			});
	
	$("#filterProdClass").live(
			'click',
			function() {
				var pk_org = $("#vendorOrg").val();
				var pk_group = $("#vendorGroup").val();
				if( (pk_org==null||pk_org=="")  ){
					showCfmMsg(getLanMsgs("0ec22180-100163"), getLanMsgs("0ec22180-100165"));
					return;
				}
				
				var myts = new Date();
				var formStr = $('#prod_class_filter').serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/register/qryTopLevelMarBasCls?myts=" + myts +"&pk_org=" + pk_org + "&pk_group="+pk_group,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						if (result != null && result != "[]") {
							var arr = eval(result);
							$("#prodcate_tree_list").empty();
							for ( var loop = 0; loop < arr.length; loop++) {
								var obj= arr[loop];
								var a = $("<a></a>").attr(
														"href", "#").addClass(
														"prodcate_category_trigger")
														.attr("margin", 0);
								var pk = $("<input />").val(obj.pk_marbasclass).attr("type", "hidden").attr("name", "prodcate_intree");
								var inputObj = $("<input/>")
														.val(obj.pk_marbasclass)
														.attr("type", "checkbox").attr("name", "pkProdcateCheckbox");
								if(obj.isLeaf == "N"){
									inputObj.attr("disabled", "true");
								}
								var label = $("<label></label>")
														.text(obj.name).attr("title", "prodcatename").attr("qaDesc", obj.qaDescription);
								
								var div = $("<div></div>")
														.addClass("prodcate_tree_category")
														.append(a).append(pk).append(inputObj).append(label);
								var existProdCl = $("#complateProducts").find("[name='pkProdCl']");
								if(existProdCl != null && existProdCl.length !=0){
									for(var l=0; l<existProdCl.length; l++){
										if($(existProdCl[l]).val() == arr[loop].pk_marbasclass){
											break;
										}
									}
									if(l == existProdCl.length){
										$("#prodcate_tree_list").append(div);
									}
								}else{
									$("#prodcate_tree_list").append(div);
								}
							}
							$("#prodcatePicker").find("#prodPkChecked").find("input").remove();
						}else{
							$("#prodcate_tree_list").empty();
						}
					}
				});
			});
	
	$("#addProduct").UIDropDialog( {
		'triggerClass' : "idTreePicker",
		'content' : $("#prodcatePicker")
	});

	
	$(".prodcate_category_trigger").live(
			'click',
			function() {
				
				var category_triggerObj=$(this);
				if(category_triggerObj.hasClass("close")){
					category_triggerObj.parent(".prodcate_tree_category").next(".children").slideUp(function(){
						category_triggerObj.removeClass("close");
					});
					return false;
				}
				
				$(this).addClass("close");
				
				var pk_org = $("#vendorOrg").val();
				var pk_group = $("#vendorGroup").val();
				if( (pk_org==null||pk_org=="")  ){
					showCfmMsg(getLanMsgs("0ec22180-100163"), getLanMsgs("0ec22180-100165"));
					return;
				}
				var pk_parent = category_triggerObj.next().val();
				
				var myts = new Date();
				var formStr = $('#prod_class_filter').serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context
							+ "/register/qryNextLevelMarBasClsByPkParent?pk_parent="+pk_parent+"&myts=" + myts+"&pk_org="+pk_org+"&pk_group="+pk_group,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
					
						if(result == "[]"){
							return false;
						}
						var orgArr = eval(result);
						
						var current_margin_left_size = parseInt(category_triggerObj.attr("margin")) + left_margin_step;	
						
						var childrenDiv = $("<div></div>").addClass("children");
						for ( var loop = 0; loop < orgArr.length; loop++) {
							var obj = orgArr[loop];

							var a = $("<a></a>").attr("href", "#").addClass(
									"prodcate_category_trigger").attr("style","margin-left:"+current_margin_left_size+"px").attr("margin", current_margin_left_size);
							var pk = $("<input />").val(obj.pk_marbasclass).attr("type", "hidden").attr("name", "prodcate_intree");
							var inputObj = $("<input/>").val(obj.pk_marbasclass).attr(
									"type", "checkbox").attr("name", "pkProdcateCheckbox");
							//展开
							$("#prodPkChecked").find("input").each(function(){
								if(obj.pk_marbasclass == $(this).val()){
									inputObj.attr("checked", "checked");
								}
							});
							
							if(obj.isLeaf == "N"){
								inputObj.attr("disabled", "true");
							}
							var label = $("<label></label>").text(obj.name).attr("title", "prodcatename").attr("qaDesc", obj.qaDescription);
							
							var div = $("<div></div>")
								.addClass("prodcate_tree_category").append(a).append(pk).append(inputObj).append(label);
												
							var existProdCl = $("#complateProducts").find("[name='pkProdCl']");
							if(existProdCl != null && existProdCl.length !=0){
								for(var l=0; l<existProdCl.length; l++){
									if($(existProdCl[l]).val() == orgArr[loop].pk_marbasclass){
										break;
									}
								}
								if(l == existProdCl.length){
									childrenDiv.append(div);
								}
							}else{
								childrenDiv.append(div);
							}
						}
						childrenDiv.insertAfter(category_triggerObj.parent());
					}
				});
				
				 category_triggerObj.parent(".prodcate_tree_category").siblings(".prodcate_tree_category").next(".children").slideUp(function(){
					 $(this).prev(".prodcate_tree_category").find(".prodcate_category_trigger").removeClass("close");
				 });

				return false;
					
			});
	
	
	$("#prodcatePicked").live('click', function() {
		//var items = $(this).parents(".ui-drop_dialog").find(":checked");
		var items = $(this).parents(".ui-drop_dialog").find("#prodPkChecked").find("input");
		
		if (items != null && items.length > 0) {
			for (var loop = 0; loop < items.length; loop++) {
				
				var newrow = $($("#prodcate_tpl").find("tr")[0]).clone();
				
				var prodcatename = $(items[loop]).attr("prodname");
				var prodqa = $(items[loop]).attr("qaDesc");
				var pkProdCl = $(items[loop]).val();
				
				$(newrow.find("[name='editProd']")[0]).val(getLanMsgs("0ec22180-100026"));
				$(newrow.find("[name='prodClName']")[0]).val(prodcatename);
				$(newrow.find("[name='pkProdCl']")[0]).val(pkProdCl);
				$(newrow.find("[name='prodName']")[0]).val(getLanMsgs("0ec22180-100166"));
				$(newrow.find("[name='pkProdv']")[0]).val("ALL");
				$(newrow.find("[name='prod_memo']")[0]).val("");
				$(newrow.find("[name='qaDescription']")[0]).val(prodqa);
				
				$(newrow.find("[name='prodPicker']")[0]).hide();
				
				$("#complateProducts").find("table").find("tbody").append(newrow);
			}
		}
		
		$(this).parents(".ui-drop_dialog").slideUp();
		return false;
	});

	$("#prodcateCancel").live('click', function() {
		$(this).parents(".ui-drop_dialog").slideUp();
		return false;
	});

	this.init = function() {
		$("body").delegate("[name='pkProdcateCheckbox']", 'click', function(){
			//this:是一个checkbox,IE10中,checkbox的值为on/off;而不是实际的val;
			//为兼容IE10,在checkbox前隐藏了一个input,值为所选主键,所以这里用$(this).prev().val();
			var val = $(this).prev().val();
			if($(this).attr("checked") == "checked"){
				var name = $(this).next("label").text();
				var qaDesc = $(this).next("label").attr("qaDesc");
				$("#prodPkChecked").append($("<input></input>").attr("id", val).val(val).attr("type","hidden").attr("prodname", name).attr("qaDesc",qaDesc));
			}else{
				$("#"+val).remove();
			}
		});
	}
}

$(function() {
	var prodcate = new ProdCate();
	prodcate.init();
});
